<?php

namespace app\api\controller;

use app\BaseController;
use app\model\WorkTag;
use app\model\Staff as StaffModel;
use app\api\validate\Staff as StaffValidate;
use think\exception\ValidateException;

class Staff extends BaseController
{
    /**
     * 员工填写信息
     *
     * @return void
     */
    public function fillInfo()
    {
        $info = [
            'boss_id'               => input('boss_id'),
            'name'                  => input('name'),
            'sex'                   => input('sex'),
            'id_num'                => input('id_num'),
            'brith_time'            => input('brith_time'),
            'age'                   => input('age'),
            'phone'                 => input('phone'),
            'census_address'        => input('census_address'),
            'dwell_address'         => input('dwell_address'),
            'dwell_address_details' => input('dwell_address_details'),
            'before_work'           => input('before_work'),
            'compensation'          => input('compensation'),
            'expect_work'           => input('expect_work'),
            'is_ill'                => input('is_ill'),
            'ill_details'           => input('ill_details'),
            'jishu_prove'           => input('jishu_prove'),
        ];

        // 校验数据
        try {
            validate(StaffValidate::class)->check($info);
        } catch (ValidateException $e) {
            error($e->getError());
        }

        if (!empty('census_address') && is_array($info['census_address'])) {
            $info['census_address'] = json_encode($info['census_address']);
        }
        if (!empty('dwell_address') && is_array($info['dwell_address'])) {
            $info['dwell_address'] = json_encode($info['dwell_address']);
        }
        if (!empty('expect_work') && is_array($info['expect_work'])) {
            $info['expect_work'] = json_encode($info['expect_work']);
        }
        if (!empty('jishu_prove') && is_array($info['jishu_prove'])) {
            $info['jishu_prove'] = json_encode($info['jishu_prove']);
        }

        try {
            StaffModel::create($info);
        } catch (\Exception $e) {
            error($e->getMessage());
        }

        success('提交成功');
    }

    /**
     * 岗位
     *
     * @return void
     */
    public function workTag()
    {
        $list = WorkTag::field('id, tag')->select();
        success('岗位列表', $list);
    }
}